<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->library('ion_auth');
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->database();
		$this->load->helper(array('url','language'));
		$this->lang->load('admin');
	}
    
	function index()
    {
		if (!$this->ion_auth->logged_in())
		{
			//redirect them to the login page
			redirect('account/login', 'refresh');
		}
		elseif (!$this->ion_auth->is_admin())
		{
			//redirect them to the home page because they must be an administrator to view this
			redirect($this->config->item('base_url'), 'refresh');
		}
		else
		{	
			$this->data['title'] = lang('admin_index_title');
			$this->data['view'] = 'admin/admin_index.php';
			$this->load->view('admin/admin_template', $this->data);
		}
    }	


    function users()
    {
			if (!$this->ion_auth->logged_in())
			{
				//redirect them to the login page
				redirect('account/login', 'refresh');
			}
			elseif (!$this->ion_auth->is_admin())
			{
				//redirect them to the home page because they must be an administrator to view this
				redirect($this->config->item('base_url'), 'refresh');
			}
			else
			{
				//set the flash data error message if there is one
				$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

				//list the users
				$this->data['users'] = $this->ion_auth->users()->result();
				foreach ($this->data['users'] as $k => $user)
				{
					$this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
				}
		
				$this->data['title'] = lang('admin_users_title');
				$this->data['view'] = 'admin/users';
				$this->load->view('admin/admin_template', $this->data);
			}
    }
  
  //deactivate the user
  function deactivate_user($id = NULL)
  {
    // no funny business, force to integer
    $id = (int) $id;

    // do we have the right userlevel?
    if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
    {
      $this->ion_auth->deactivate($id);
    }
    $this->session->set_flashdata('message', 'Account Deactivated');
    //redirect them back to the admin/users page
    redirect('admin/users', 'refresh');

  }
	
  //activate the user
  function activate_user($id)
  {

    // no funny business, force to integer
    $id = (int) $id;

    // do we have the right userlevel?
    if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
    {
      $this->ion_auth->activate($id);
    }
    $this->session->set_flashdata('message', 'Account Activated');
    //redirect them back to the admin/users page
    redirect('admin/users', 'refresh');
  }

  //delete a user
	function delete_user($id)
	{
		if (!$this->ion_auth->logged_in())
		{
			//redirect them to the login page
			redirect('auth/login', 'refresh');
		}
		elseif (!$this->ion_auth->is_admin())
		{
			//redirect them to the home page because they must be an administrator to view this
			redirect($this->config->item('base_url'), 'refresh');
		}
		else //logged in and admin, it's ok.
		{
			if($this->ion_auth->delete_user($id))
			{
				$this->session->set_flashdata('message', "User Deleted");
			}
			else
			{
				$this->session->set_flashdata('message', "User NOT Deleted");
			}
			redirect('admin/users','refresh');
		}	
	}
    
	//create a new user
	function create_user()
	{
		$this->data['title'] = "Create User";

		if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
		{
			redirect('account/login', 'refresh');
		}

		//validate form input
    $this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
		$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
		$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
		$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
		$this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
		$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');

		if ($this->form_validation->run() == true)
		{
			$username = $this->input->post('username');
			$email = $this->input->post('email');
			$password = $this->input->post('password');

			$additional_data = array('first_name' => $this->input->post('first_name'),
				'last_name' => $this->input->post('last_name')
			);
		}
		if ($this->form_validation->run() == true && $this->ion_auth->register($username, $password, $email, $additional_data))
		{ //check to see if we are creating the user
			//redirect them back to the admin page
			$this->session->set_flashdata('message', "User Created");
			redirect("admin/users", 'refresh');
		}
		else
		{ //display the create user form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

            $this->data['username'] = array('name' => 'username',
				'id' => 'username',
				'type' => 'text',
				'value' => $this->form_validation->set_value('username'),
			);
            
			$this->data['first_name'] = array('name' => 'first_name',
				'id' => 'first_name',
				'type' => 'text',
				'value' => $this->form_validation->set_value('first_name'),
			);
			$this->data['last_name'] = array('name' => 'last_name',
				'id' => 'last_name',
				'type' => 'text',
				'value' => $this->form_validation->set_value('last_name'),
			);
			$this->data['email'] = array('name' => 'email',
				'id' => 'email',
				'type' => 'text',
				'value' => $this->form_validation->set_value('email'),
			);
            $this->data['group'] = array('name' => 'group',
				'id' => 'group',
				'type' => 'text',
				'value' => $this->form_validation->set_value('email'),
			);
            
            $this->data['group_options'] = $this->ion_auth->groups();
            
			$this->data['password'] = array('name' => 'password',
				'id' => 'password',
				'type' => 'password',
				'value' => $this->form_validation->set_value('password'),
			);
			$this->data['password_confirm'] = array('name' => 'password_confirm',
				'id' => 'password_confirm',
				'type' => 'password',
				'value' => $this->form_validation->set_value('password_confirm'),
			);
			
			$this->data['view'] = 'admin/create_user';
			$this->load->view('admin/admin_template', $this->data);
		}
	}    

    function riders($action = NULL)
    {
			if (!$this->ion_auth->logged_in())
			{
				//redirect them to the login page
				redirect('account/login', 'refresh');
			}
			elseif (!$this->ion_auth->is_admin())
			{
				//redirect them to the home page because they must be an administrator to view this
				redirect($this->config->item('base_url'), 'refresh');
			}
			else
			{
        //list the riders
        $this->load->model('riders_model');
        $this->load->helper('form');
        $this->data['riders'] = $this->riders_model->get_all();
        
        if($action == 'save')
        {
          foreach($this->data['riders'] as $rider)
          {
            $post_data = $this->input->post();
            
            if(isset($post_data['starter'][$rider->rider_id]) && $rider->starter == 0) //rider will start the Tour
            {
              $this->riders_model->set_starter($rider->rider_id);
            }
            if(!isset($post_data['starter'][$rider->rider_id]) && $rider->starter == 1) //rider will not start the Tour
            {
              $this->riders_model->set_no_starter($rider->rider_id);
              $this->riders_model->set_inactive($rider->rider_id);
            }

            if(isset($post_data['active'][$rider->rider_id]) && $rider->active == 0) //rider became active
            {
              $this->riders_model->set_active($rider->rider_id);
              $this->riders_model->set_starter($rider->rider_id);
            }
            if(!isset($post_data['active'][$rider->rider_id]) && $rider->active == 1) //rider became inactive
            {
              $this->riders_model->set_inactive($rider->rider_id);
            }
          }
          redirect('admin/riders','refresh'); //refresh the view, to show the updates
        }

				
        foreach($this->data['riders'] as $rider)
        {
          $this->data['starter'][$rider->rider_id] = array(
                                                            'name' => 'starter['.$rider->rider_id.']',
                                                            'id' =>'starter['.$rider->rider_id.']',
                                                            'value' => 'starter',
                                                            'checked' => $rider->starter
                                                           );
          $this->data['active'][$rider->rider_id] = array(
                                                            'name' => 'active['.$rider->rider_id.']',
                                                            'id' =>'active['.$rider->rider_id.']',
                                                            'value' => 'active',
                                                            'checked' => $rider->active
                                                           );
        }

        $this->data['title'] = lang('admin_riders_title');
        $this->data['help_message_title'] = 'help_riders_title';
        $this->data['help_message_text'] = 'help_riders_text';
				$this->data['view'] = 'admin/riders';
				$this->load->view('admin/admin_template', $this->data);
			}
    }

    function edit_rider($rider_id)
    {
      $this->data['title'] = lang('edit_rider');

      if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
      {
        redirect('account/login', 'refresh');
      }
      if($this->input->get('continue'))
      {
        $this->session->set_userdata('continue', $this->input->get('continue'));
      }
      //validate form input
      $this->form_validation->set_rules('number', lang('number'), 'required|numeric|xss_clean');
      $this->form_validation->set_rules('last_name', lang('name'), 'required|xss_clean');
      $this->form_validation->set_rules('first_name', lang('first_name'), 'required|xss_clean');
      $this->form_validation->set_rules('country', lang('country'), 'required|alpha_dash|xss_clean');

      $this->load->model('riders_model');
      $this->rider = $this->riders_model->get_by_id($rider_id);

      if ($this->form_validation->run() == true)
      {
        //update rider info
        $attributes = $this->input->post();
        if(!isset($attributes['starter']))
        {
          $attributes['starter'] = 0;
        }
        if(!isset($attributes['active']))
        {
          $attributes['active'] = 0;
        }
        $this->riders_model->update($rider_id, $attributes);
        $this->session->set_flashdata('message', lang('saved'));

        if ($this->session->userdata('continue')) 
        {
          $continue = $this->session->userdata('continue');
          $this->session->unset_userdata('continue');
          redirect($continue); 
        }
        else
        {
         redirect('admin/riders', 'refresh');
        }
      }
      else
      { //display the rider edit form
        //set the flash data error message if there is one
        $this->load->helper('form');
        $this->data['message'] = validation_errors();

        $this->data['number'] = array('name' => 'number',
          'id' => 'number',
          'type' => 'text',
          'value' => $this->rider->number
        );
              
        $this->data['last_name'] = array('name' => 'last_name',
          'id' => 'last_name',
          'type' => 'text',
          'value' => $this->rider->last_name
        );

        $this->data['first_name'] = array('name' => 'first_name',
          'id' => 'first_name',
          'type' => 'text',
          'value' => $this->rider->first_name
        );

        $this->data['starter'] = array(
          'name' => 'starter',
          'id' => 'starter',
          'value' => '1',
          'checked' => $this->rider->starter
        );

        $this->data['active'] = array(
          'name' => 'active',
          'id' => 'active',
          'value' => '1',
          'checked' => $this->rider->active
        ); 

        $this->data['country'] = array('name' => 'country',
          'id' => 'country',
          'type' => 'text',
          'value' => $this->rider->country
        );        

        $types = $this->riders_model->get_types();
        foreach($types as $type)
        {
          $type_options[$type['type_id']] = lang($type['type_description']);
        }

        $this->load->model('teams_model');
        $teams = $this->teams_model->get_all();
        foreach($teams as $team)
        {
          $team_options[$team->team_id] = $team->team_name." (".$team->team_shortname.")";
        }



        $this->data['rider'] = $this->rider;
        $this->data['rider_type_options'] = $type_options;
        $this->data['team_options'] = $team_options;
        $this->data['view'] = 'admin/edit_rider';
        $this->load->view('admin/admin_template', $this->data);
      }
    }

    function teams()
    {
      if (!$this->ion_auth->logged_in())
      {
        //redirect them to the login page
        redirect('account/login', 'refresh');
      }
      elseif (!$this->ion_auth->is_admin())
      {
        //redirect them to the home page because they must be an administrator to view this
        redirect($this->config->item('base_url'), 'refresh');
      }
      else
      {

        //list the riders
        $this->load->model(array('teams_model', 'riders_model'));
        $this->load->helper('date');
        $this->data['teams'] = $this->teams_model->get_all();
        $i = 0;
        foreach($this->data['teams'] as $team)
        {
          $riders = $this->riders_model->get_by_team_number($team->team_number);
          $this->data['teams'][$i]->team_riders = $riders;
          $i++;
        }
        
        $this->data['title'] = lang('admin_teams');
        $this->data['view'] = 'admin/teams';
        $this->load->view('admin/admin_template', $this->data);
      }
    }

    function edit_team($team_number)
    {
      $this->data['title'] = lang('edit_team');

      if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
      {
        redirect('account/login', 'refresh');
      }

      //validate form input
      $this->form_validation->set_rules('team_name', 'Team Name', 'required|xss_clean');
      $this->form_validation->set_rules('team_shortname', 'Team Shortname', 'required|alpha_numeric|exact_length[3]|xss_clean');

      $this->load->model('teams_model');
      $this->team = $this->teams_model->get_by_number($team_number);

      if ($this->form_validation->run() == true)
      {
        //update team info
        $attributes = $this->input->post();
        $this->teams_model->update($team_number, $attributes);
        $this->session->set_flashdata('message', lang('saved'));
        redirect('admin/edit_team/'.$team_number, 'refresh');
      }
      else
      { //display the team edit form
        //set the flash data error message if there is one
        $this->load->helper('form');
        $this->data['message'] = validation_errors();

        $this->data['team_name'] = array(
          'name' => 'team_name',
          'id' => 'team_name',
          'type' => 'text',
          'value' => $this->team->team_name
        );
              
        $this->data['team_shortname'] = array(
          'name' => 'team_shortname',
          'id' => 'team_shortname',
          'type' => 'text',
          'value' => $this->team->team_shortname
        );

        $this->data['team_country'] = array(
          'name' => 'team_country',
          'id' => 'team_country',
          'type' => 'text',
          'value' => $this->team->team_country
        );        
        $this->load->model('riders_model');
        $this->data['riders'] = $this->riders_model->get_all_by_team_number($this->team->team_number);
        $this->data['team_number'] = $this->team->team_number;
        $this->data['view'] = 'admin/edit_team';
        $this->load->view('admin/admin_template', $this->data);
      }     

    }

    function edit_team_save()
    {
      //print_r($this->input->post());
      $this->load->model('riders_model');
      $starters = $this->input->post('start_rider');
      $actives = $this->input->post('active_rider');
      foreach($starters as $id => $start)
      {
        $this->riders_model->update($id, array('starter' => $start, 'active' => $actives[$id]));
      }

      if(!tour_started()) //don't do this after the tour has started
      {
        //get all user selections
        $this->load->model('selections_model');
        $selections = $this->selections_model->get_all();
            
        // check if anyone has a non-starter on their team, then replace it with a replacement, but only if the tour has not started yet.
        foreach($starters as $id =>$start)
        {
          if($start == 0) // rider is not starting
          {
            foreach($selections as $user_selection) // check for every user
            {
              $sel_array = explode(',', $user_selection->selection);
              if(in_array($id, $sel_array)) // non-starter in there?
              {
                $key = array_search($id, $sel_array);
                unset($sel_array[$key]); // pop out the non-starter
                $rep_array = explode(',', $user_selection->replacements);
                $sel_array[] = $rep_array[0]; // stick the first replacement rider in there
                unset($rep_array[0]); // take the replacing rider out of the replacements
                // now update the whole thing
                $this->selections_model->update($user_selection->user_id, array('selection'=> trim(implode(',',$sel_array),' ,'),'replacements'=> trim(implode(',',$rep_array),' ,')));
              }
            }

          }
        }
      }

      redirect('admin/edit_team/'.$this->input->post('team_number'), 'refresh');
    }

    function stages()
    {
			if (!$this->ion_auth->logged_in())
			{
				//redirect them to the login page
				redirect('account/login', 'refresh');
			}
			elseif (!$this->ion_auth->is_admin())
			{
				//redirect them to the home page because they must be an administrator to view this
				redirect($this->config->item('base_url'), 'refresh');
			}
			else
			{

				//list the riders
				$this->load->model('stages_model');
                $this->load->helper('date');
				$this->data['stages'] = $this->stages_model->get_all();
				$this->data['title'] = lang('admin_stages_title');
				$this->data['view'] = 'admin/stages';
				$this->load->view('admin/admin_template', $this->data);
			}
    }

    function edit_stage($stage_id)
    {
    	$this->data['title'] = lang('edit_stage');

			if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
			{
				redirect('account/login', 'refresh');
			}

			//validate form input
	    $this->form_validation->set_rules('stage_id', 'Stage Number', 'required|is_natural|xss_clean');
			$this->form_validation->set_rules('stage_type', 'Stage Type', 'required|alpha_dash|xss_clean');
			$this->form_validation->set_rules('stage_date', 'Stage Date', 'required|xss_clean');
			$this->form_validation->set_rules('stage_depart', 'Stage Depart', 'required|xss_clean');
			$this->form_validation->set_rules('stage_arrive', 'Stage Arrive', 'required|xss_clean');
			$this->form_validation->set_rules('stage_distance', 'Stage Distance', 'required|numeric|xss_clean');

			$this->load->model('stages_model');
			$this->stage = $this->stages_model->get_by_id($stage_id);

			if ($this->form_validation->run() == true)
			{
				//update stage info
				$attributes = $this->input->post();
				$this->stages_model->update($stage_id, $attributes);
				$this->session->set_flashdata('message', lang('saved'));
				redirect('admin/edit_stage/'.$stage_id, 'refresh');
			}
			else
			{ //display the stage edit form
				//set the flash data error message if there is one
				$this->load->helper('form');
				$this->data['message'] = validation_errors();

	      $this->data['stage_id'] = array('name' => 'stage_id',
					'id' => 'stage_id',
					'type' => 'text',
					'value' => $this->stage->stage_id
				);
	            
				$this->data['stage_type'] = array('name' => 'stage_type',
					'id' => 'stage_type',
					'type' => 'text',
					'value' => $this->stage->stage_type
				);

				$this->data['stage_type_options'] = array('prologue' => lang('prologue'),
																									'plain' =>  lang('plain'),
																									'medium_mountains' =>  lang('medium_mountains'),
																									'high_mountains' =>  lang('high_mountains'),
																									'individual_time_trial' =>  lang('individual_time_trial'),
																									);

				$this->data['stage_date'] = array('name' => 'stage_date',
					'id' => 'stage_date',
					'type' => 'text',
					'value' => $this->stage->stage_date,
					'class' => 'datepicker'
				);
				$this->data['stage_depart'] = array('name' => 'stage_depart',
					'id' => 'stage_depart',
					'type' => 'text',
					'value' => $this->stage->stage_depart
				);
				$this->data['stage_arrive'] = array('name' => 'stage_arrive',
					'id' => 'stage_arrive',
					'type' => 'text',
					'value' => $this->stage->stage_arrive
				);				
				$this->data['stage_distance'] = array('name' => 'stage_distance',
					'id' => 'stage_distance',
					'type' => 'text',
					'value' => $this->stage->stage_distance
				);				
				
				$this->data['view'] = 'admin/edit_stage';
				$this->load->view('admin/admin_template', $this->data);
			}
    }

    function delete_stage($stage_id)
    {
			if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
			{
				redirect('account/login', 'refresh');
			}

			$this->load->model('stages_model');
			$this->stages_model->delete($stage_id);
			redirect('admin/stages','refresh');    	
    }

    function delete_all_results()
    {
      if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
      {
        redirect('account/login', 'refresh');
      }

      $this->load->model('stages_model');
      $this->stages_model->delete_all_results();
      redirect('admin/stages','refresh');     
    }

    function edit_result($stage_id)
    {
      $this->data['title'] = lang('edit_result');

      if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
      {
        redirect('account/login', 'refresh');
      }

      $this->load->model('stages_model');

      $this->data['stage'] = $this->stages_model->get_by_id($stage_id);

      //validate form input
      $this->form_validation->set_rules('stage_result[1]', 'Position', 'integer|xss_clean');

      if ($this->form_validation->run() == true)
      {
        
        $stage_result_array = $this->input->post('stage_result');
        $stage_yellow_array = $this->input->post('stage_yellow');
        $stage_green_array = $this->input->post('stage_green');
        $stage_mountain_array = $this->input->post('stage_mountain');

        if (is_array($stage_result_array))   { $attributes['stage_result'] = implode(',', $stage_result_array); }
        if (is_array($stage_yellow_array))   { $attributes['stage_yellow'] = implode(',', $stage_yellow_array); }
        if (is_array($stage_green_array))    { $attributes['stage_green'] = implode(',', $stage_green_array); }
        if (is_array($stage_mountain_array)) { $attributes['stage_mountain'] = implode(',', $stage_mountain_array); }

        $this->stages_model->update($stage_id, $attributes);

        //update Result info
         $this->load->library('tourpoule');
         $calc = $this->tourpoule->calculate_stage($stage_id);
        
        
         $this->session->set_flashdata('message', 'Saved & Calculated '.$calc['counter'].' users in '.$calc['time_elapsed'].' seconds.');
         redirect('admin/edit_result/'.$stage_id, 'refresh');
      }
      else
      { //display the result edit form
        //set the flash data error message if there is one
        $this->load->helper('form');
        $this->data['message'] = validation_errors();

        $this->load->model(array('riders_model', 'points_model'));
        $riders = $this->riders_model->get_all_starters();

        //see if there are results for this one already
        $this->data['top_10'] = $this->points_model->get_top_10($stage_id);

        $this->data['rider_options'][0] = "";
        foreach($riders as $rider)
        {
          $this->data['rider_options'][$rider->rider_id] = $rider->number." - ".$rider->last_name." ".$rider->first_name." (".$rider->team_shortname.")";
        }

    if ($stage_id != 20)
    {
      $points_stage           = $this->config_model->item('points_stage');
      $points_stage_yellow    = $this->config_model->item('points_stage_yellow');
      $points_stage_green     = $this->config_model->item('points_stage_green');
      $points_stage_mountain  = $this->config_model->item('points_stage_mountain');
    }
    else
    {
      $points_stage           = $this->config_model->item('points_stage');
      $points_stage_yellow    = $this->config_model->item('points_end_yellow');
      $points_stage_green     = $this->config_model->item('points_end_green');
      $points_stage_mountain  = $this->config_model->item('points_end_mountain');
    }


        if($this->config_model->item('points_stage') != 0)
        {
          if(strstr($points_stage, ','))
          {
            $this->data['points_stage'] = explode(',',$points_stage);
          }
          else
          {
            $this->data['points_stage'][0] = $points_stage;
          }
          $this->data['stage_result'] = array_fill(0, count($this->data['points_stage']), 0);
        }  
        
        if($points_stage_yellow != 0)
        {
          if(strstr($points_stage_yellow, ','))
          {
            $this->data['points_stage_yellow'] = explode(',',$points_stage_yellow);
          }
          else
          {
            $this->data['points_stage_yellow'][0] = points_stage_yellow;
          }
          $this->data['stage_yellow'] = array_fill(0, count($this->data['points_stage_yellow']), 0);
        }

        if($points_stage_green != 0)
        {
          if(strstr($points_stage_green, ','))
          {
            $this->data['points_stage_green'] = explode(',',$points_stage_green);
          }
          else
          {
            $this->data['points_stage_green'][0] = $points_stage_green;
          }
          $this->data['stage_green'] = array_fill(0, count($this->data['points_stage_green']), 0);
        }

        if($points_stage_mountain != 0)
        {
          if(strstr($points_stage_mountain, ','))
          {
            $this->data['points_stage_mountain'] = explode(',',$points_stage_mountain);
          }
          else
          {
            $this->data['points_stage_mountain'][0] = $points_stage_mountain;
          }
          $this->data['stage_mountain'] = array_fill(0, count($this->data['points_stage_mountain']), 0);
        }

        // If there is already a result in the database, replace the 0's with the actual result
        if ($this->data['stage']->stage_result != "" && strstr($this->data['stage']->stage_result, ','))
        {
          $this->data['stage_result'] = array_join($this->data['stage_result'], explode(',', $this->data['stage']->stage_result));
        }
        elseif ($this->data['stage']->stage_result != "" && $this->data['stage']->stage_result != NULL)
        {  
          $this->data['stage_result'][0] = $this->data['stage']->stage_result;
        }

        if ($this->data['stage']->stage_yellow != "" && strstr($this->data['stage']->stage_yellow, ','))
        {
          $this->data['stage_yellow'] = array_join($this->data['stage_yellow'], explode(',', $this->data['stage']->stage_yellow));
        }
        elseif ($this->data['stage']->stage_yellow != "" && $this->data['stage']->stage_yellow != NULL)
        {  
          $this->data['stage_yellow'][0] = $this->data['stage']->stage_yellow;
        }

        if ($this->data['stage']->stage_green != "" && strstr($this->data['stage']->stage_green, ','))
        {
          $this->data['stage_green'] = array_join($this->data['stage_green'], explode(',', $this->data['stage']->stage_green));
        }
        elseif ($this->data['stage']->stage_green != "" && $this->data['stage']->stage_green != NULL)
        {  
          $this->data['stage_green'][0] = $this->data['stage']->stage_green;
        }

        if ($this->data['stage']->stage_mountain != "" && strstr($this->data['stage']->stage_mountain, ','))
        {
          $this->data['stage_mountain'] = array_join($this->data['stage_mountain'], explode(',', $this->data['stage']->stage_mountain));
        }
        elseif ($this->data['stage']->stage_mountain != "" && $this->data['stage']->stage_mountain != NULL)
        {  
          $this->data['stage_mountain'][0] = $this->data['stage']->stage_mountain;
        }


        $this->data['help_message_title'] = 'help_results_title';
        $this->data['help_message_text'] = 'help_results_text';        
        $this->data['view'] = 'admin/edit_result';
        $this->load->view('admin/admin_template', $this->data);
      }
    }


    function all_tables()
    {
    	$this->riders_table();
    	$this->stages_table();
    	$this->teams_table();
      $this->rider_types_table();
      $this->selections_table();
          	echo "ready";
    }

    function riders_table()
    {
    	$sql_query[] = "DROP TABLE IF EXISTS `riders`";
    	$sql_query[] = "CREATE TABLE IF NOT EXISTS `riders` (
										  `rider_id` int(11) NOT NULL AUTO_INCREMENT,
										  `number` varchar(4) NOT NULL,
										  `first_name` varchar(255) NOT NULL,
										  `last_name` varchar(255) NOT NULL,
										  `team_number` int(11) NOT NULL,
										  `type_id` int(11) NOT NULL,
										  `country` varchar(20) NOT NULL,
										  `active` tinyint(4) NOT NULL,
										  `starter` tinyint(4) NOT NULL,
										  PRIMARY KEY (`rider_id`),
										  UNIQUE KEY `number` (`number`)
										) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1" ;

      $sql_query[] = "REPLACE INTO `riders`
      								(`number`,	`first_name`,	`last_name`,	`team_number`,`type_id`,`country`,`active`,`starter`)
                			VALUES
                			('1001','Manuel','BELLETTI',1,1,'ITA',1,1),
                      ('1002','Julien','BERARD',1,1,'FRA',1,1),
                      ('1003','Guillaume','BONNAFOND',1,1,'FRA',1,1),
                      ('1004','Maxime','BOUET',1,1,'FRA',1,1),
                      ('1005','Jimmy','CASPER',1,1,'FRA',1,1),
                      ('1006','Mikael','CHEREL',1,1,'FRA',1,1),
                      ('1007','Hubert','DUPONT',1,1,'FRA',1,1),
                      ('1008','Martin','ELMIGER',1,1,'SUI',1,1),
                      ('1009','John','GADRET',1,1,'FRA',1,1),
                      ('1010','Ben','GASTAUER',1,1,'LUX',1,1),
                      ('1011','Gregor','GAZVODA',1,1,'SLO',1,1),
                      ('1012','Sylvain','GEORGES',1,1,'FRA',1,1),
                      ('1013','Kristof','GODDAERT',1,1,'BEL',1,1),
                      ('1014','Sébastien','HINAULT',1,1,'FRA',1,1),
                      ('1015','Steve','HOUANARD',1,1,'FRA',1,1),
                      ('1016','Blel','KADRI',1,1,'FRA',1,1),
                      ('1017','Romain','LEMARCHAND',1,1,'FRA',1,1),
                      ('1018','Sébastien','MINARD',1,1,'FRA',1,1),
                      ('1019','Lloyd','MONDORY',1,1,'FRA',1,1),
                      ('1020','Matteo','MONTAGUTI',1,1,'ITA',1,1),
                      ('1021','Rinaldo','NOCENTINI',1,1,'ITA',1,1),
                      ('1022','Jean-Christophe','PERAUD',1,1,'FRA',1,1),
                      ('1023','Mathieu','PERGET',1,1,'FRA',1,1),
                      ('1024','Anthony','RAVARD',1,1,'FRA',1,1),
                      ('1025','Christophe','RIBLON',1,1,'FRA',1,1),
                      ('1026','Nicolas','ROCHE',1,1,'IRL',1,1),
                      ('1027','Boris','SHPILEVSKY',1,1,'RUS',1,1),
                      ('1028','Amir','ZARGARI',1,1,'IRI',1,1),
                      ('1029','Romain','BARDET',1,1,'FRA',1,1),
                      ('1030','Assan','BAZAYEV',2,1,'KAZ',1,1),
                      ('1031','Borut','BOZIC',2,1,'SLO',1,1),
                      ('1032','Janez','BRAJKOVIC',2,1,'SLO',1,1),
                      ('1033','Alexsandr','DYACHENKO',2,1,'KAZ',1,1),
                      ('1034','Dmitriy','FOFONOV',2,1,'KAZ',1,1),
                      ('1035','Enrico','GASPAROTTO',2,1,'ITA',1,1),
                      ('1036','Francesco','GAVAZZI',2,1,'ITA',1,1),
                      ('1037','Andriy','GRIVKO',2,1,'UKR',1,1),
                      ('1038','Dmitriy','GRUZDEV',2,1,'KAZ',1,1),
                      ('1039','Jacopo','GUARNIERI',2,1,'ITA',1,1),
                      ('1040','Maxim','IGLINSKIY',2,1,'KAZ',1,1),
                      ('1041','Valentin','IGLINSKIY',2,1,'KAZ',1,1),
                      ('1042','Tanel','KANGERT',2,1,'EST',1,1),
                      ('1043','Andrey','KASHECHKIN',2,1,'KAZ',1,1),
                      ('1044','Fredrik Carl Wilhelm','KESSIAKOFF',2,1,'SWE',1,1),
                      ('1045','Robert','KISERLOVSKI',2,1,'CRO',1,1),
                      ('1046','Roman','KREUZIGER',2,1,'CZE',1,1),
                      ('1047','Dmitriy','MURAVYEV',2,1,'KAZ',1,1),
                      ('1048','Yevgeniy','NEPOMNYACHSHIY',2,1,'KAZ',1,1),
                      ('1049','Evgeny','PETROV',2,1,'RUS',1,1),
                      ('1050','Simone','PONZI',2,1,'ITA',1,1),
                      ('1051','Sergey','RENEV',2,1,'KAZ',1,1),
                      ('1052','Kevin','SEELDRAEYERS',2,1,'BEL',1,1),
                      ('1053','Egor','SILIN',2,1,'RUS',1,1),
                      ('1054','Paolo','TIRALONGO',2,1,'ITA',1,1),
                      ('1055','Alexandr','VINOKUROV',2,1,'KAZ',1,1),
                      ('1056','Andrey','ZEITS',2,1,'KAZ',1,1),
                      ('1057','Alessandro','BALLAN',3,1,'ITA',1,1),
                      ('1058','Adam','BLYTHE',3,1,'GBR',1,1),
                      ('1059','Brent','BOOKWALTER',3,1,'USA',1,1),
                      ('1060','Marcus','BURGHARDT',3,1,'GER',1,1),
                      ('1061','Stephen','CUMMINGS',3,1,'GBR',1,1),
                      ('1062','Cadel','EVANS',3,1,'AUS',1,1),
                      ('1063','Mathias','FRANK',3,1,'SUI',1,1),
                      ('1064','Philippe','GILBERT',3,1,'BEL',1,1),
                      ('1065','George','HINCAPIE',3,1,'USA',1,1),
                      ('1066','Thor','HUSHOVD',3,1,'NOR',1,1),
                      ('1067','Martin','KOHLER',3,1,'SUI',1,1),
                      ('1068','Klaas','LODEWYCK',3,1,'BEL',1,1),
                      ('1069','Amaël','MOINARD',3,1,'FRA',1,1),
                      ('1070','Steve','MORABITO',3,1,'SUI',1,1),
                      ('1071','Marco','PINOTTI',3,1,'ITA',1,1),
                      ('1072','Manuel','QUINZIATO',3,1,'ITA',1,1),
                      ('1073','Mauro','SANTAMBROGIO',3,1,'ITA',1,1),
                      ('1074','Ivan','SANTAROMITA',3,1,'ITA',1,1),
                      ('1075','Michael','SCHÄR',3,1,'SUI',1,1),
                      ('1076','Johann','TSCHOPP',3,1,'SUI',1,1),
                      ('1077','Greg','VAN AVERMAET',3,1,'BEL',1,1),
                      ('1078','Tejay','VAN GARDEREN',3,1,'USA',1,1),
                      ('1079','Danilo','WYSS',3,1,'SUI',1,1),
                      ('1080','Yannick','EIJSSEN',3,1,'BEL',1,1),
                      ('1081','Taylor','PHINNEY',3,1,'USA',1,1),
                      ('1082','Thimothy','ROE',3,1,'AUS',1,1),
                      ('1083','Igor','ANTON HERNANDEZ ',4,1,'ESP',1,1),
                      ('1084','Mikel','ASTARLOZA CHAURREAU ',4,1,'ESP',1,1),
                      ('1085','Jorge','AZANZA SOTO ',4,1,'ESP',1,1),
                      ('1086','Victor','CABEDO',4,1,'ESP',1,1),
                      ('1087','Pierre','CAZAUX',4,1,'FRA',1,1),
                      ('1088','Ricardo','GARCIA AMBROA ',4,1,'ESP',1,1),
                      ('1089','Gorka','IZAGUIRRE INSAUSTI ',4,1,'ESP',1,1),
                      ('1090','ESTEBAN Egoi','MARTINEZ DE ',4,1,'ESP',1,1),
                      ('1091','Miguel','MINGUEZ AYALA ',4,1,'ESP',1,1),
                      ('1092','Mikel','NIEVE ITURALDE ',4,1,'ESP',1,1),
                      ('1093','Juan Jose','OROZ UGALDE ',4,1,'ESP',1,1),
                      ('1094','Alan','PEREZ LEZAUN ',4,1,'ESP',1,1),
                      ('1095','Ruben','PEREZ MORENO ',4,1,'ESP',1,1),
                      ('1096','Adrian','SAEZ',4,1,'ESP',1,1),
                      ('1097','Samuel','SANCHEZ GONZALEZ ',4,1,'ESP',1,1),
                      ('1098','Romain','SICARD',4,1,'FRA',1,1),
                      ('1099','Amets','TXURRUKA',4,1,'ESP',1,1),
                      ('1100','Pablo','URTASUN PEREZ ',4,1,'ESP',1,1),
                      ('1101','Ivan','VELASCO MURILLO ',4,1,'ESP',1,1),
                      ('1102','Gorka','VERDUGO MARCOTEGUI ',4,1,'ESP',1,1),
                      ('1103','Peio','BILBAO',4,1,'ESP',1,1),
                      ('1104','Jon','IZAGUIRRE INSAUSTI ',4,1,'ESP',1,1),
                      ('1105','Mikel','LANDA MEANA ',4,1,'ESP',1,1),
                      ('1106','William','BONNET',5,1,'FRA',1,1),
                      ('1107','David','BOUCHER',5,1,'FRA',1,1),
                      ('1108','Sandy','CASAR',5,1,'FRA',1,1),
                      ('1109','Steve','CHAINEL',5,1,'FRA',1,1),
                      ('1110','Mickael','DELAGE',5,1,'FRA',1,1),
                      ('1111','Pierrick','FEDRIGO',5,1,'FRA',1,1),
                      ('1112','Arnaud','GERARD',5,1,'FRA',1,1),
                      ('1113','Anthony','GESLIN',5,1,'FRA',1,1),
                      ('1114','Yauheni','HUTAROVICH',5,1,'BLR',1,1),
                      ('1115','Arnold','JEANNESSON',5,1,'FRA',1,1),
                      ('1116','Matthieu','LADAGNOUS',5,1,'FRA',1,1),
                      ('1117','Francis','MOUREY',5,1,'FRA',1,1),
                      ('1118','Yoann','OFFREDO',5,1,'FRA',1,1),
                      ('1119','Rémi','PAURIOL',5,1,'FRA',1,1),
                      ('1120','Cédric','PINEAU',5,1,'FRA',1,1),
                      ('1121','Thibaut','PINOT',5,1,'FRA',1,1),
                      ('1122','Gabriel','RASCH',5,1,'NOR',1,1),
                      ('1123','Dominique','ROLLIN',5,1,'CAN',1,1),
                      ('1124','Anthony','ROUX',5,1,'FRA',1,1),
                      ('1125','Jérémy','ROY',5,1,'FRA',1,1),
                      ('1126','Geoffrey','SOUPE',5,1,'FRA',1,1),
                      ('1127','Benoît','VAUGRENARD',5,1,'FRA',1,1),
                      ('1128','Jussi','VEIKKANEN',5,1,'FIN',1,1),
                      ('1129','Arthur','VICHOT',5,1,'FRA',1,1),
                      ('1130','Nacer','BOUHANNI',5,1,'FRA',1,1),
                      ('1131','Arnaud','COURTEILLE',5,1,'FRA',1,1),
                      ('1132','Arnaud','DEMARE',5,1,'FRA',1,1),
                      ('1133','Kenny','ELISSONDE',5,1,'FRA',1,1),
                      ('1134','Jack','BAUER',6,1,'NZL',1,1),
                      ('1135','Thomas','DANIELSON',6,1,'USA',1,1),
                      ('1136','Thomas','DEKKER',6,1,'NED',1,1),
                      ('1137','Tyler','FARRAR',6,1,'USA',1,1),
                      ('1138','Koldo','FERNANDEZ',6,1,'ESP',1,1),
                      ('1139','Murilo Antonio','FISCHER',6,1,'BRA',1,1),
                      ('1140','Nathan','HAAS',6,1,'AUS',1,1),
                      ('1141','Heinrich','HAUSSLER',6,1,'AUS',1,1),
                      ('1142','Ryder','HESJEDAL',6,1,'CAN',1,1),
                      ('1143','Alex','HOWES',6,1,'USA',1,1),
                      ('1144','Robert','HUNTER',6,1,'RSA',1,1),
                      ('1145','Andreas','KLIER',6,1,'GER',1,1),
                      ('1146','Michel','KREDER',6,1,'NED',1,1),
                      ('1147','Christophe','LE MEVEL ',6,1,'FRA',1,1),
                      ('1148','Martijn','MAASKANT',6,1,'NED',1,1),
                      ('1149','Daniel','MARTIN',6,1,'IRL',1,1),
                      ('1150','David','MILLAR',6,1,'GBR',1,1),
                      ('1151','Ramunas','NAVARDAUSKAS',6,1,'LTU',1,1),
                      ('1152','Thomas','PETERSON',6,1,'USA',1,1),
                      ('1153','Alex','RASMUSSEN',6,1,'DEN',1,1),
                      ('1154','Sébastien','ROSSELER',6,1,'BEL',1,1),
                      ('1155','Peter','STETINA',6,1,'USA',1,1),
                      ('1156','Andrew','TALANSKY',6,1,'USA',1,1),
                      ('1157','Johan','VAN SUMMEREN ',6,1,'BEL',1,1),
                      ('1158','Christian','VANDEVELDE',6,1,'USA',1,1),
                      ('1159','Sep','VANMARCKE',6,1,'BEL',1,1),
                      ('1160','Fabian','WEGMANN',6,1,'GER',1,1),
                      ('1161','David','ZABRISKIE',6,1,'USA',1,1),
                      ('1162','Raymond','KREDER',6,1,'NED',1,1),
                      ('1163','Jacob','RATHE',6,1,'USA',1,1),
                      ('1164','Michael','ALBASINI',7,1,'SUI',1,1),
                      ('1165','Fumiyuki','BEPPU',7,1,'JPN',1,1),
                      ('1166','Sam','BEWLEY',7,1,'NZL',1,1),
                      ('1167','Jack','BOBRIDGE',7,1,'AUS',1,1),
                      ('1168','Simon','CLARKE',7,1,'AUS',1,1),
                      ('1169','Baden','COOKE',7,1,'AUS',1,1),
                      ('1170','Allan','DAVIS',7,1,'AUS',1,1),
                      ('1171','Julian','DEAN',7,1,'NZL',1,1),
                      ('1172','Mitchell','DOCKER',7,1,'AUS',1,1),
                      ('1173','Simon','GERRANS',7,1,'AUS',1,1),
                      ('1174','Matthew Harley','GOSS',7,1,'AUS',1,1),
                      ('1175','Leigh','HOWARD',7,1,'AUS',1,1),
                      ('1176','Daryl','IMPEY',7,1,'RSA',1,1),
                      ('1177','Jens','KEUKELEIRE',7,1,'BEL',1,1),
                      ('1178','Aidis','KRUOPIS',7,1,'LTU',1,1),
                      ('1179','Brett','LANCASTER',7,1,'AUS',1,1),
                      ('1180','Sebastian','LANGEVELD',7,1,'NED',1,1),
                      ('1181','Christian','MEIER',7,1,'CAN',1,1),
                      ('1182','Travis','MEYER',7,1,'AUS',1,1),
                      ('1183','Cameron','MEYER',7,1,'AUS',1,1),
                      ('1184','Jens','MOURIS',7,1,'NED',1,1),
                      ('1185','Stuart','O’GRADY',7,1,'AUS',1,1),
                      ('1186','Wesley','SULZBERGER',7,1,'AUS',1,1),
                      ('1187','Daniel','TEKLEHAIMANOT',7,1,'ERI',1,1),
                      ('1188','Svein','TUFT',7,1,'CAN',1,1),
                      ('1189','Tomas','VAITKUS',7,1,'LTU',1,1),
                      ('1190','Pieter','WEENING',7,1,'NED',1,1),
                      ('1191','Matthew','WILSON',7,1,'AUS',1,1),
                      ('1192','Luke','DURBRIDGE',7,1,'AUS',1,1),
                      ('1193','Michael','HEPBURN',7,1,'AUS',1,1),
                      ('1194','Maxim','BELKOV',8,1,'RUS',1,1),
                      ('1195','Pavel','BRUTT',8,1,'RUS',1,1),
                      ('1196','Giampaolo','CARUSO',8,1,'ITA',1,1),
                      ('1197','Xavier','FLORENCIO CABRE ',8,1,'ESP',1,1),
                      ('1198','Oscar','FREIRE GOMEZ ',8,1,'ESP',1,1),
                      ('1199','Vladimir','GUSEV',8,1,'RUS',1,1),
                      ('1200','Juan','HORRACH RIPPOLL ',8,1,'ESP',1,1),
                      ('1201','Petr','IGNATENKO',8,1,'RUS',1,1),
                      ('1202','Mikhail','IGNATYEV',8,1,'RUS',1,1),
                      ('1203','Vladimir','ISAICHEV',8,1,'RUS',1,1),
                      ('1204','Alexandr','KOLOBNEV',8,1,'RUS',1,1),
                      ('1205','Alexander','KRISTOFF',8,1,'NOR',1,1),
                      ('1206','Timofey','KRITSKIY',8,1,'RUS',1,1),
                      ('1207','Aliaksandr','KUSCHYNSKI',8,1,'BLR',1,1),
                      ('1208','Alberto','LOSADA ALGUACIL ',8,1,'ESP',1,1),
                      ('1209','Denis','MENCHOV',8,1,'RUS',1,1),
                      ('1210','Daniel','MORENO FERNANDEZ ',8,1,'ESP',1,1),
                      ('1211','Luca','PAOLINI',8,1,'ITA',1,1),
                      ('1212','Alexander','PORSEV',8,1,'RUS',1,1),
                      ('1213','Joaquin','RODRIGUEZ OLIVER ',8,1,'ESP',1,1),
                      ('1214','Rudiger','SELIG',8,1,'GER',1,1),
                      ('1215','Gatis','SMUKULIS',8,1,'LAT',1,1),
                      ('1216','Simon','SPILAK',8,1,'SLO',1,1),
                      ('1217','Yury','TROFIMOV',8,1,'RUS',1,1),
                      ('1218','Alexey','TSATEVITCH',8,1,'RUS',1,1),
                      ('1219','Maxime','VANTOMME',8,1,'BEL',1,1),
                      ('1220','Angel','VICIOSO ARCOS ',8,1,'ESP',1,1),
                      ('1221','Eduard','VORGANOV',8,1,'RUS',1,1),
                      ('1222','Marco','HALLER',8,1,'AUT',1,1),
                      ('1223','Winner','ANACONA GOMEZ ',9,1,'COL',1,1),
                      ('1224','Leonardo','BERTAGNOLLI',9,1,'ITA',1,1),
                      ('1225','Grega','BOLE',9,1,'SLO',1,1),
                      ('1226','Matteo','BONO',9,1,'ITA',1,1),
                      ('1227','Vitaliy','BUTS',9,1,'UKR',1,1),
                      ('1228','Davide','CIMOLAI',9,1,'ITA',1,1),
                      ('1229','Damiano','CUNEGO',9,1,'ITA',1,1),
                      ('1230','Massimo','GRAZIATO',9,1,'ITA',1,1),
                      ('1231','Danilo','HONDO',9,1,'GER',1,1),
                      ('1232','Denys','KOSTYUK',9,1,'UKR',1,1),
                      ('1233','Dmytro','KRIVTSOV',9,1,'UKR',1,1),
                      ('1234','Yuriy','KRIVTSOV',9,1,'FRA',1,1),
                      ('1235','Oleksandr','KVACHUK',9,1,'UKR',1,1),
                      ('1236','Matthew','LLOYD',9,1,'AUS',1,1),
                      ('1237','Adriano','MALORI',9,1,'ITA',1,1),
                      ('1238','Marco','MARZANO',9,1,'ITA',1,1),
                      ('1239','Manuele','MORI',9,1,'ITA',1,1),
                      ('1240','Przemyslaw','NIEMIEC',9,1,'POL',1,1),
                      ('1241','Alessandro','PETACCHI',9,1,'ITA',1,1),
                      ('1242','Daniele','PIETROPOLLI',9,1,'ITA',1,1),
                      ('1243','Morris','POSSONI',9,1,'ITA',1,1),
                      ('1244','Daniele','RIGHI',9,1,'ITA',1,1),
                      ('1245','Michele','SCARPONI',9,1,'ITA',1,1),
                      ('1246','Oleksandr','SHEYDYK',9,1,'UKR',1,1),
                      ('1247','Alessandro','SPEZIALETTI',9,1,'ITA',1,1),
                      ('1248','Simone','STORTONI',9,1,'ITA',1,1),
                      ('1249','Diego','ULISSI',9,1,'ITA',1,1),
                      ('1250','Davide','VIGANO',9,1,'ITA',1,1),
                      ('1251','Valerio','AGNOLI',10,1,'ITA',1,1),
                      ('1252','Stefano','AGOSTINI',10,1,'ITA',1,1),
                      ('1253','Ivan','BASSO',10,1,'ITA',1,1),
                      ('1254','Maciej','BODNAR',10,1,'POL',1,1),
                      ('1255','Federico','CANUTI',10,1,'ITA',1,1),
                      ('1256','Eros','CAPECCHI',10,1,'ITA',1,1),
                      ('1257','Damiano','CARUSO',10,1,'ITA',1,1),
                      ('1258','Mauro','DA DALTO ',10,1,'ITA',1,1),
                      ('1259','Tiziano','DALL’ANTONIA',10,1,'ITA',1,1),
                      ('1260','Timothy','DUGGAN',10,1,'USA',1,1),
                      ('1261','Edward','KING',10,1,'USA',1,1),
                      ('1262','Kristijan','KOREN',10,1,'SLO',1,1),
                      ('1263','Paolo','LONGO BORGHINI ',10,1,'ITA',1,1),
                      ('1264','Alan','MARANGONI',10,1,'ITA',1,1),
                      ('1265','Dominik','NERZ',10,1,'GER',1,1),
                      ('1266','Vincenzo','NIBALI',10,1,'ITA',1,1),
                      ('1267','Daniel','OSS',10,1,'ITA',1,1),
                      ('1268','Maciej','PATERSKI',10,1,'POL',1,1),
                      ('1269','Daniele','RATTO',10,1,'ITA',1,1),
                      ('1270','Fabio','SABATINI',10,1,'ITA',1,1),
                      ('1271','Peter','SAGAN',10,1,'SVK',1,1),
                      ('1272','Juraj','SAGAN',10,1,'SVK',1,1),
                      ('1273','Cristiano','SALERNO',10,1,'ITA',1,1),
                      ('1274','Cayetano José','SARMIENTO TUNARROSA ',10,1,'COL',1,1),
                      ('1275','Sylvester','SZMYD',10,1,'POL',1,1),
                      ('1276','Alessandro','VANOTTI',10,1,'ITA',1,1),
                      ('1277','Elia','VIVIANI',10,1,'ITA',1,1),
                      ('1278','Moreno','MOSER',10,1,'ITA',1,1),
                      ('1279','Lars Ytting','BAK',11,1,'DEN',1,1),
                      ('1280','Gaetan','BILLE',11,1,'BEL',1,1),
                      ('1281','Brian','BULGAC',11,1,'NED',1,1),
                      ('1282','Sander','CORDEEL',11,1,'BEL',1,1),
                      ('1283','Bart','DE CLERCQ ',11,1,'BEL',1,1),
                      ('1284','Francis','DE GREEF ',11,1,'BEL',1,1),
                      ('1285','Kenny','DE HAES ',11,1,'BEL',1,1),
                      ('1286','Gert','DOCKX',11,1,'BEL',1,1),
                      ('1287','André','GREIPEL',11,1,'GER',1,1),
                      ('1288','Adam','HANSEN',11,1,'AUS',1,1),
                      ('1289','Greg','HENDERSON',11,1,'NZL',1,1),
                      ('1290','Olivier','KAISEN',11,1,'BEL',1,1),
                      ('1291','Gianni','MEERSMAN',11,1,'BEL',1,1),
                      ('1292','Maarten','NEYENS',11,1,'BEL',1,1),
                      ('1293','Vicente','REYNES MIMO ',11,1,'ESP',1,1),
                      ('1294','Frederique','ROBERT',11,1,'BEL',1,1),
                      ('1295','Jurgen','ROELANDTS',11,1,'BEL',1,1),
                      ('1296','Marcel','SIEBERG',11,1,'GER',1,1),
                      ('1297','Mehdi','SOHRABI',11,1,'IRI',1,1),
                      ('1298','Jurgen','VAN DE WALLE ',11,1,'BEL',1,1),
                      ('1299','Jurgen','VAN DEN BROECK ',11,1,'BEL',1,1),
                      ('1300','Joost','VAN LEIJEN ',11,1,'NED',1,1),
                      ('1301','Jelle','VANENDERT',11,1,'BEL',1,1),
                      ('1302','Dennis','VANENDERT',11,1,'BEL',1,1),
                      ('1303','Jonas','VANGENECHTEN',11,1,'BEL',1,1),
                      ('1304','Frederik','WILLEMS',11,1,'BEL',1,1),
                      ('1305','Jens','DEBUSSCHERE',11,1,'BEL',1,1),
                      ('1306','Tosh','VAN DER SANDE ',11,1,'BEL',1,1),
                      ('1307','Andrey','AMADOR BAKKAZAKOVA ',12,1,'CRC',1,1),
                      ('1308','David','ARROYO DURAN ',12,1,'ESP',1,1),
                      ('1309','Marzio','BRUSEGHIN',12,1,'ITA',1,1),
                      ('1310','Jonathan','CASTROVIEJO NICOLAS ',12,1,'ESP',1,1),
                      ('1311','Juan Jose','COBO ACEBO ',12,1,'ESP',1,1),
                      ('1312','Imanol','ERVITI OLLO ',12,1,'ESP',1,1),
                      ('1313','Rui Alberto','FARIA DA COSTA ',12,1,'POR',1,1),
                      ('1314','José Ivan','GUTIERREZ PALACIOS ',12,1,'ESP',1,1),
                      ('1315','José','HERRADA LOPEZ ',12,1,'ESP',1,1),
                      ('1316','Benat','INTXAUSTI ELORRIAGA ',12,1,'ESP',1,1),
                      ('1317','Javier','IRIARTE',12,1,'ESP',1,1),
                      ('1318','Vladimir','KARPETS',12,1,'RUS',1,1),
                      ('1319','Vasili','KIRYIENKA',12,1,'BLR',1,1),
                      ('1320','Ignatas','KONOVALOVAS',12,1,'LTU',1,1),
                      ('1321','Pablo','LASTRAS GARCIA ',12,1,'ESP',1,1),
                      ('1322','David','LOPEZ GARCIA ',12,1,'ESP',1,1),
                      ('1323','Angel','MADRAZO RUIZ ',12,1,'ESP',1,1),
                      ('1324','Javier','MORENO BAZAN ',12,1,'ESP',1,1),
                      ('1325','Sergio','PARDILLA BELLON ',12,1,'ESP',1,1),
                      ('1326','Ruben','PLAZA MOLINA ',12,1,'ESP',1,1),
                      ('1327','Jose Joaquin','ROJAS GIL ',12,1,'ESP',1,1),
                      ('1328','Branislau','SAMOILAU',12,1,'BLR',1,1),
                      ('1329','Alejandro','VALVERDE BELMONTE ',12,1,'ESP',1,1),
                      ('1330','Francisco José','VENTOSO ALBERDI ',12,1,'ESP',1,1),
                      ('1331','Giovanni','VISCONTI',12,1,'ITA',1,1),
                      ('1332','Jesus','HERRADA LOPEZ ',12,1,'ESP',1,1),
                      ('1333','Nairo Alexander','QUINTANA ROJAS ',12,1,'COL',1,1),
                      ('1334','Enrique','SANZ',12,1,'ESP',1,1),
                      ('1335','Marco','BANDIERA',13,1,'ITA',1,1),
                      ('1336','Tom','BOONEN',13,1,'BEL',1,1),
                      ('1337','Matt','BRAMMEIER',13,1,'IRL',1,1),
                      ('1338','Dario','CATALDO',13,1,'ITA',1,1),
                      ('1339','Sylvain','CHAVANEL',13,1,'FRA',1,1),
                      ('1340','Francesco','CHICCHI',13,1,'ITA',1,1),
                      ('1341','Gerald','CIOLEK',13,1,'GER',1,1),
                      ('1342','Kevin','DE WEERT ',13,1,'BEL',1,1),
                      ('1343','Dries','DEVENYNS',13,1,'BEL',1,1),
                      ('1344','Michal','GOLAS',13,1,'POL',1,1),
                      ('1345','Bert','GRABSCH',13,1,'GER',1,1),
                      ('1346','Iljo','KEISSE',13,1,'BEL',1,1),
                      ('1347','Levi','LEIPHEIMER',13,1,'USA',1,1),
                      ('1348','Nikolas','MAES',13,1,'BEL',1,1),
                      ('1349','Tony','MARTIN',13,1,'GER',1,1),
                      ('1350','Serge','PAUWELS',13,1,'BEL',1,1),
                      ('1351','Jérôme','PINEAU',13,1,'FRA',1,1),
                      ('1352','Frantisek','RABON',13,1,'CZE',1,1),
                      ('1353','Gert','STEEGMANS',13,1,'BEL',1,1),
                      ('1354','Zdenek','STYBAR',13,1,'CZE',1,1),
                      ('1355','Niki','TERPSTRA',13,1,'NED',1,1),
                      ('1356','Stijn','VANDENBERGH',13,1,'BEL',1,1),
                      ('1357','Kristof','VANDEWALLE',13,1,'BEL',1,1),
                      ('1358','Martin','VELITS',13,1,'SVK',1,1),
                      ('1359','Peter','VELITS',13,1,'SVK',1,1),
                      ('1360','Andrew','FENN',13,1,'GBR',1,1),
                      ('1361','Michal','KWIATKOWSKI',13,1,'POL',1,1),
                      ('1362','Matteo','TRENTIN',13,1,'ITA',1,1),
                      ('1363','Guillaume','VAN KEIRSBULCK ',13,1,'BEL',1,1),
                      ('1364','Julien','VERMOTE',13,1,'BEL',1,1),
                      ('1365','Carlos','BARREDO LLAMAZALES ',14,1,'ESP',1,1),
                      ('1366','Jetse','BOL',14,1,'NED',1,1),
                      ('1367','Lars','BOOM',14,1,'NED',1,1),
                      ('1368','Theo','BOS',14,1,'NED',1,1),
                      ('1369','Matti','BRESCHEL',14,1,'DEN',1,1),
                      ('1370','Graeme','BROWN',14,1,'AUS',1,1),
                      ('1371','Stef','CLEMENT',14,1,'NED',1,1),
                      ('1372','Rick','FLENS',14,1,'NED',1,1),
                      ('1373','Juan Manuel','GARATE',14,1,'ESP',1,1),
                      ('1374','Robert','GESINK',14,1,'NED',1,1),
                      ('1375','Steven','KRUIJSWIJK',14,1,'NED',1,1),
                      ('1376','Thomas','LEEZER',14,1,'NED',1,1),
                      ('1377','Paul','MARTENS',14,1,'GER',1,1),
                      ('1378','Bauke','MOLLEMA',14,1,'NED',1,1),
                      ('1379','Grischa','NIERMANN',14,1,'GER',1,1),
                      ('1380','Mark','RENSHAW',14,1,'AUS',1,1),
                      ('1381','Luis Leon','SANCHEZ GIL ',14,1,'ESP',1,1),
                      ('1382','Bram','TANKINK',14,1,'NED',1,1),
                      ('1383','Laurens','TEN DAM ',14,1,'NED',1,1),
                      ('1384','Maarten','TJALLINGII',14,1,'NED',1,1),
                      ('1385','Jos','VAN EMDEN ',14,1,'NED',1,1),
                      ('1386','Dennis','VAN WINDEN ',14,1,'NED',1,1),
                      ('1387','Coen','VERMELTFOORT',14,1,'NED',1,1),
                      ('1388','Maarten','WYNANTS',14,1,'BEL',1,1),
                      ('1389','Wilco','KELDERMAN',14,1,'NED',1,1),
                      ('1390','Michael','MATTHEWS',14,1,'AUS',1,1),
                      ('1391','Tom Jelte','SLAGTER',14,1,'NED',1,1),
                      ('1392','Jan','BAKELANTS',15,1,'BEL',1,1),
                      ('1393','Daniele','BENNATI',15,1,'ITA',1,1),
                      ('1394','Matthew','BUSCHE',15,1,'USA',1,1),
                      ('1395','Fabian','CANCELLARA',15,1,'SUI',1,1),
                      ('1396','Laurent','DIDIER',15,1,'LUX',1,1),
                      ('1397','Jakob','FUGLSANG',15,1,'DEN',1,1),
                      ('1398','Tony','GALLOPIN',15,1,'FRA',1,1),
                      ('1399','Linus','GERDEMANN',15,1,'GER',1,1),
                      ('1400','Ben','HERMANS',15,1,'BEL',1,1),
                      ('1401','Christopher','HORNER',15,1,'USA',1,1),
                      ('1402','Markel','IRIZAR ARANBURU ',15,1,'ESP',1,1),
                      ('1403','Benjamin','KING',15,1,'USA',1,1),
                      ('1404','Andreas','KLÖDEN',15,1,'GER',1,1),
                      ('1405','Tiago','MACHADO',15,1,'POR',1,1),
                      ('1406','Maxime','MONFORT',15,1,'BEL',1,1),
                      ('1407','Yaroslav','POPOVYCH',15,1,'UKR',1,1),
                      ('1408','Joost','POSTHUMA',15,1,'NED',1,1),
                      ('1409','Grégory','RAST',15,1,'SUI',1,1),
                      ('1410','Thomas','ROHREGGER',15,1,'AUT',1,1),
                      ('1411','Hayden','ROULSTON',15,1,'NZL',1,1),
                      ('1412','Nelson Filipe','SANTOS SIMOES OLIVEIRA ',15,1,'POR',1,1),
                      ('1413','Andy','SCHLECK',15,1,'LUX',1,1),
                      ('1414','Frank','SCHLECK',15,1,'LUX',1,1),
                      ('1415','Jesse','SERGENT',15,1,'NZL',1,1),
                      ('1416','Jens','VOIGT',15,1,'GER',1,1),
                      ('1417','Robert','WAGNER',15,1,'GER',1,1),
                      ('1418','Oliver','ZAUGG',15,1,'SUI',1,1),
                      ('1419','Haimar','ZUBELDIA AGIRRE ',15,1,'ESP',1,1),
                      ('1420','George','BENNETT',15,1,'NZL',1,1),
                      ('1421','Giacomo','NIZZOLO',15,1,'ITA',1,1),
                      ('1422','Davide','APPOLLONIO',16,1,'ITA',1,1),
                      ('1423','Michael','BARRY',16,1,'CAN',1,1),
                      ('1424','Edvald','BOASSON HAGEN ',16,1,'NOR',1,1),
                      ('1425','Mark','CAVENDISH',16,1,'GBR',1,1),
                      ('1426','Alex','DOWSETT',16,1,'GBR',1,1),
                      ('1427','Bernhard','EISEL',16,1,'AUT',1,1),
                      ('1428','Juan Antonio','FLECHA GIANNONI ',16,1,'ESP',1,1),
                      ('1429','Christopher','FROOME',16,1,'GBR',1,1),
                      ('1430','Mathew','HAYMAN',16,1,'AUS',1,1),
                      ('1431','Sergio Luis','HENAO MONTOYA ',16,1,'COL',1,1),
                      ('1432','Jeremy','HUNT',16,1,'GBR',1,1),
                      ('1433','Peter','KENNAUGH',16,1,'GBR',1,1),
                      ('1434','Christian','KNEES',16,1,'GER',1,1),
                      ('1435','Thomas','LÖVKVIST',16,1,'SWE',1,1),
                      ('1436','Lars Petter','NORDHAUG',16,1,'NOR',1,1),
                      ('1437','Danny','PATE',16,1,'USA',1,1),
                      ('1438','Richie','PORTE',16,1,'AUS',1,1),
                      ('1439','Salvatore','PUCCIO',16,1,'ITA',1,1),
                      ('1440','Michael','ROGERS',16,1,'AUS',1,1),
                      ('1441','Kanstantsin','SIVTSOV',16,1,'BLR',1,1),
                      ('1442','Ian','STANNARD',16,1,'GBR',1,1),
                      ('1443','Christopher','SUTTON',16,1,'AUS',1,1),
                      ('1444','Ben','SWIFT',16,1,'GBR',1,1),
                      ('1445','Geraint','THOMAS',16,1,'GBR',1,1),
                      ('1446','Rigoberto','URAN URAN ',16,1,'COL',1,1),
                      ('1447','Bradley','WIGGINS',16,1,'GBR',1,1),
                      ('1448','Xabier','ZANDIO ECHAIDE ',16,1,'ESP',1,1),
                      ('1449','Luke','ROWE',16,1,'GBR',1,1),
                      ('1450','Manuele','BOARO',17,1,'ITA',1,1),
                      ('1451','Jonathan','CANTWELL',17,1,'AUS',1,1),
                      ('1452','Mads','CHRISTENSEN',17,1,'DEN',1,1),
                      ('1453','Volodymir','GUSTOV',17,1,'UKR',1,1),
                      ('1454','Juan José','HAEDO',17,1,'ARG',1,1),
                      ('1455','Lucas Sebastian','HAEDO',17,1,'ARG',1,1),
                      ('1456','Jesus','HERNANDEZ BLAZQUEZ ',17,1,'ESP',1,1),
                      ('1457','Jonas Aaen','JÖRGENSEN',17,1,'DEN',1,1),
                      ('1458','Christopher','JUUL JENSEN ',17,1,'DEN',1,1),
                      ('1459','Kasper','KLOSTERGAARD LARSEN ',17,1,'DEN',1,1),
                      ('1460','Karsten','KROON',17,1,'NED',1,1),
                      ('1461','Anders','LUND',17,1,'DEN',1,1),
                      ('1462','Ran','MARGALIOT',17,1,'ISR',1,1),
                      ('1463','Jaroslaw','MARYCZ',17,1,'POL',1,1),
                      ('1464','Takashi','MIYAZAWA',17,1,'JPN',1,1),
                      ('1465','Michael','MORKOV',17,1,'DEN',1,1),
                      ('1466','Daniel','NAVARRO GARCIA ',17,1,'ESP',1,1),
                      ('1467','Benjamin','NOVAL GONZALEZ ',17,1,'ESP',1,1),
                      ('1468','Nick','NUYENS',17,1,'BEL',1,1),
                      ('1469','Sergio Miguel Moreira','PAULINHO',17,1,'POR',1,1),
                      ('1470','Bruno','PIRES',17,1,'POR',1,1),
                      ('1471','Luke','ROBERTS',17,1,'AUS',1,1),
                      ('1472','Nicki','SÖRENSEN',17,1,'DEN',1,1),
                      ('1473','Chris Anker','SÖRENSEN',17,1,'DEN',1,1),
                      ('1474','David','TANNER',17,1,'AUS',1,1),
                      ('1475','Matteo','TOSATTO',17,1,'ITA',1,1),
                      ('1476','Troels Ronning','VINTHER',17,1,'DEN',1,1),
                      ('1477','Rafal','MAJKA',17,1,'POL',1,1),
                      ('1478','Kris','BOECKMANS',18,1,'BEL',1,1),
                      ('1479','Matteo','CARRARA',18,1,'ITA',1,1),
                      ('1480','Thomas','DE GENDT ',18,1,'BEL',1,1),
                      ('1481','Stefan','DENIFL',18,1,'AUT',1,1),
                      ('1482','Stijn','DEVOLDER',18,1,'BEL',1,1),
                      ('1483','Romain','FEILLU',18,1,'FRA',1,1),
                      ('1484','Johnny','HOOGERLAND',18,1,'NED',1,1),
                      ('1485','Martijn','KEIZER',18,1,'NED',1,1),
                      ('1486','Sergey','LAGUTIN',18,1,'UZB',1,1),
                      ('1487','Gustav','LARSSON',18,1,'SWE',1,1),
                      ('1488','Björn','LEUKEMANS',18,1,'BEL',1,1),
                      ('1489','Pim','LIGTHART',18,1,'NED',1,1),
                      ('1490','Bertjan','LINDEMAN',18,1,'NED',1,1),
                      ('1491','Marco','MARCATO',18,1,'ITA',1,1),
                      ('1492','Tomasz','MARCZYNSKI',18,1,'POL',1,1),
                      ('1493','Wouter','MOL',18,1,'NED',1,1),
                      ('1494','Jacek','MORAJKO',18,1,'POL',1,1),
                      ('1495','Martin','MORTENSEN',18,1,'DEN',1,1),
                      ('1496','Nikita','NOVIKOV',18,1,'RUS',1,1),
                      ('1497','Marcello','PAVARIN',18,1,'ITA',1,1),
                      ('1498','Wouter','POELS',18,1,'NED',1,1),
                      ('1499','Rob','RUIJGH',18,1,'NED',1,1),
                      ('1500','Mirko','SELVAGGI',18,1,'ITA',1,1),
                      ('1501','Rafael','VALLS FERRI ',18,1,'ESP',1,1),
                      ('1502','Kenny Robert','VAN HUMMEL ',18,1,'NED',1,1),
                      ('1503','Frederik','VEUCHELEN',18,1,'BEL',1,1),
                      ('1504','Lieuwe','WESTRA',18,1,'NED',1,1),
                      ('1505','Barry','MARKUS',18,1,'NED',1,1),
                      ('1506','Roy','CURVERS',19,1,'NED ',1,1),
                      ('1507','Bert','DE BACKER ',19,1,'BEL ',1,1),
                      ('1508','Koen','DE KORT ',19,1,'NED ',1,1),
                      ('1509','Yukihiro','DOI',19,1,'JPN ',1,1),
                      ('1510','Johannes','FRÖHLINGER',19,1,'GER ',1,1),
                      ('1511','Alexandre','GENIEZ',19,1,'FRA ',1,1),
                      ('1512','Simon','GESCHKE',19,1,'GER ',1,1),
                      ('1513','Patrick','GRETSCH',19,1,'GER ',1,1),
                      ('1514','Yann','HUGUET',19,1,'FRA ',1,1),
                      ('1515','Thierry','HUPOND',19,1,'FRA ',1,1),
                      ('1516','Cheng','JI',19,1,'CHN ',1,1),
                      ('1517','Marcel','KITTEL',19,1,'GER ',1,1),
                      ('1518','Dominic','KLEMME',19,1,'GER ',1,1),
                      ('1519','Roger','KLUGE',19,1,'GER ',1,1),
                      ('1520','Ramon','SINKELDAM',19,1,'NED ',1,1),
                      ('1521','Matthieu','SPRICK',19,1,'FRA ',1,1),
                      ('1522','Tom','STAMSNIJDER',19,1,'NED ',1,1),
                      ('1523','Albert','TIMMER',19,1,'NED ',1,1),
                      ('1524','Ronan','VAN ZANDBEEK ',19,1,'NED ',1,1),
                      ('1525','Tom','VEELERS',19,1,'NED ',1,1),
                      ('1526','Thomas','BONNIN',19,1,'FRA ',1,1),
                      ('1527','Thomas','DAMUSEAU',19,1,'FRA ',1,1),
                      ('1528','John','DEGENKOLB',19,1,'GER ',1,1),
                      ('1529','Tom','DUMOULIN',19,1,'NED ',1,1),
                      ('1530','Tobias','LUDVIGSSON',19,1,'SWE ',1,1),
                      ('1531','Yohan','BAGOT',20,1,'FRA ',1,1),
                      ('1532','Florent','BARLE',20,1,'FRA ',1,1),
                      ('1533','Mickaël','BUFFAZ',20,1,'FRA ',1,1),
                      ('1534','Edwig','CAMMAERTS',20,1,'BEL ',1,1),
                      ('1535','Jean-Eudes','DEMARET',20,1,'FRA ',1,1),
                      ('1536','GREGORIO Rémy','DI',20,1,'FRA ',1,1),
                      ('1537','Samuel','DUMOULIN',20,1,'FRA ',1,1),
                      ('1538','Leonardo Fabio','DUQUE',20,1,'COL ',1,1),
                      ('1539','Nicolas','EDET',20,1,'FRA ',1,1),
                      ('1540','Julien','FOUCHARD',20,1,'FRA ',1,1),
                      ('1541','ECHEGUIBEL Egoitz','GARCIA',20,1,'ESP ',1,1),
                      ('1542','Jan','GHYSELINCK',20,1,'BEL ',1,1),
                      ('1543','Arnaud','LABBE',20,1,'FRA ',1,1),
                      ('1544','MARDONES Luis Angel','MATE',20,1,'ESP ',1,1),
                      ('1545','Rudy','MOLARD',20,1,'FRA ',1,1),
                      ('1546','David','MONCOUTIE',20,1,'FRA ',1,1),
                      ('1547','Damien','MONIER',20,1,'FRA ',1,1),
                      ('1548','Aleksejs','SARAMOTINS',20,1,'LAT ',1,1),
                      ('1549','Nico','SIJMENS',20,1,'BEL ',1,1),
                      ('1550','Rein','TAARAMAE',20,1,'EST ',1,1),
                      ('1551','Tristan','VALENTIN',20,1,'FRA ',1,1),
                      ('1552','Nicolas','VOGONDY',20,1,'FRA ',1,1),
                      ('1553','Romain','ZINGLE',20,1,'BEL ',1,1),
                      ('1554','Adrien','PETIT',20,1,'FRA',1,1),
                      ('1555','Cyril','BESSY',21,1,'FRA ',1,1),
                      ('1556','Jerome','COPPEL',21,1,'FRA ',1,1),
                      ('1557','Arnaud','COYOT',21,1,'FRA ',1,1),
                      ('1558','Anthony','DELAPLACE',21,1,'FRA ',1,1),
                      ('1559','Jimmy','ENGOULVENT',21,1,'FRA ',1,1),
                      ('1560','Brice','FEILLU',21,1,'FRA ',1,1),
                      ('1561','Jérémie','GALLAND',21,1,'FRA ',1,1),
                      ('1562','Jonathan','HIVERT',21,1,'FRA ',1,1),
                      ('1563','Fabrice','JEANDESBOZ',21,1,'FRA ',1,1),
                      ('1564','Christophe','LABORIE',21,1,'FRA ',1,1),
                      ('1565','David','LELAY',21,1,'FRA ',1,1),
                      ('1566','Cyril','LEMOINE',21,1,'FRA ',1,1),
                      ('1567','Guillaume','LEVARLET',21,1,'FRA ',1,1),
                      ('1568','Laurent','MANGEL',21,1,'FRA ',1,1),
                      ('1569','Jean Marc','MARINO',21,1,'FRA ',1,1),
                      ('1570','Rony','MARTIAS',21,1,'FRA ',1,1),
                      ('1571','Maxime','MEDEREL',21,1,'FRA ',1,1),
                      ('1572','Jean-Lou','PAIANI',21,1,'FRA ',1,1),
                      ('1573','Stéphane','POULHIES',21,1,'FRA ',1,1),
                      ('1574','Paul','POUX',21,1,'FRA ',1,1),
                      ('1575','Julien','SIMON',21,1,'FRA ',1,1),
                      ('1576','Yannick','TALABARDON',21,1,'FRA ',1,1),
                      ('1577','Etienne','TORTELIER',21,1,'FRA ',1,1),
                      ('1578','Yukiya','ARASHIRO',22,1,'JPN ',1,1),
                      ('1579','Giovanni','BERNAUDEAU',22,1,'FRA ',1,1),
                      ('1580','Franck','BOUYER',22,1,'FRA ',1,1),
                      ('1581','Anthony','CHARTEAU',22,1,'FRA ',1,1),
                      ('1582','Sébastien','CHAVANEL',22,1,'FRA ',1,1),
                      ('1583','Rafaa','CHTIOUI',22,1,'TUN ',1,1),
                      ('1584','Mathieu','CLAUDE',22,1,'FRA ',1,1),
                      ('1585','Damien','GAUDIN',22,1,'FRA ',1,1),
                      ('1586','Cyril','GAUTIER',22,1,'FRA ',1,1),
                      ('1587','Yohann','GENE',22,1,'FRA ',1,1),
                      ('1588','Saïd','HADDOU',22,1,'FRA ',1,1),
                      ('1589','Tony','HUREL',22,1,'FRA ',1,1),
                      ('1590','Vincent','JEROME',22,1,'FRA ',1,1),
                      ('1591','Christophe','KERN',22,1,'FRA ',1,1),
                      ('1592','Davide','MALACARNE',22,1,'ITA ',1,1),
                      ('1593','Alexandre','PICHOT',22,1,'FRA ',1,1),
                      ('1594','Perrig','QUEMENEUR',22,1,'FRA ',1,1),
                      ('1595','Kévin','REZA',22,1,'FRA ',1,1),
                      ('1596','Pierre','ROLLAND',22,1,'FRA ',1,1),
                      ('1597','Bjorn','THURAU',22,1,'GER ',1,1),
                      ('1598','Sébastien','TURGOT',22,1,'FRA ',1,1),
                      ('1599','David','VEILLEUX',22,1,'CAN ',1,1),
                      ('1600','Thomas','VOECKLER',22,1,'FRA ',1,1),
                      ('1601','Jerome','COUSIN',22,1,'FRA ',1,1),
                      ('1602','Matteo','PELUCCHI',22,1,'ITA ',1,1),
                      ('1603','Angelo','TULIK',22,1,'FRA ',1,1)";

        foreach ($sql_query as $query)
        {
            $q = $this->db->query($query);
        }

    }

    function stages_table()
    {
    	$sql_query[] = "DROP TABLE IF EXISTS `stages`";
			$sql_query[] = "CREATE TABLE IF NOT EXISTS `stages` (
											  `stage_id` int(11) NOT NULL,
											  `stage_type` varchar(255) NOT NULL,
											  `stage_date` DATE NOT NULL,
											  `stage_depart` varchar(255) NOT NULL,
											  `stage_arrive` varchar(255) NOT NULL,
											  `stage_distance` FLOAT NOT NULL,
											  PRIMARY KEY (`stage_id`)
											) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1" ;

      $sql_query[] = "REPLACE INTO `stages`
      								(`stage_id`, `stage_type`, `stage_date`, `stage_depart`, `stage_arrive`, `stage_distance`)
                			VALUES
                			(0, 'prologue',								'2012-06-30', 'Liège', 'Liège', 6.4),
                			(1, 'plain',									'2012-07-01', 'Liège', 'Seraing', 198),
                			(2, 'plain',									'2012-07-02', 'Visé', 'Tournai', 207.5),
                			(3, 'medium_mountains',				'2012-07-03', 'Orchies', 'Boulogne-sur-Mer', 197),
                			(4, 'plain',									'2012-07-04', 'Abbeville', 'Rouen', 214.5),
                			(5, 'plain',									'2012-07-05', 'Rouen', 'Saint-Quentin', 196.5),
                			(6, 'plain',									'2012-07-06', 'Épernay', 'Metz', 207.5),
                			(7, 'medium_mountains',				'2012-07-07', 'Tomblaine', 'La Planche des Belles Filles', 199),
                			(8, 'medium_mountains',				'2012-07-08', 'Belfort', 'Porrentruy', 157.5),
                			(9, 'individual_time_trial',	'2012-07-09', 'Arc-et-Senans', 'Besançon', 41.5),
                			(10, 'high_mountains',				'2012-07-11', 'Mâcon', 'Bellegarde-sur-Valserine', 194.5),
                			(11, 'high_mountains',				'2012-07-12', 'Albertville', 'La Toussuire - Les Sybelles', 148),
                			(12, 'medium_mountains',			'2012-07-13', 'Saint-Jean-de-Maurienne', 'Annonay Davézieux', 226),
                			(13, 'plain',									'2012-07-14', 'Saint-Paul-Trois-Châteaux', 'Le Cap d’Agde', 217),
                			(14, 'high_mountains',				'2012-07-15', 'Limoux', 'Foix', 191),
                			(15, 'plain',									'2012-07-16', 'Samatan', 'Pau', 158.5),
                			(16, 'high_mountains',				'2012-07-18', 'Pau', 'Bagnères-de-Luchon', 197),
                			(17, 'high_mountains',				'2012-07-19', 'Bagnères-de-Luchon', 'Peyragudes', 143.5),
                			(18, 'plain',									'2012-07-20', 'Blagnac', 'Brive-la-Gaillarde', 222.5),
                			(19, 'individual_time_trial',	'2012-07-21', 'Bonneval', 'Chartres', 53.5),
                			(20, 'plain',									'2012-07-22', 'Rambouillet', 'Paris Champs-Élysées', 120)
                			";

      $sql_query[] = "DROP TABLE IF EXISTS `results`";
			$sql_query[] = "CREATE TABLE IF NOT EXISTS `results` (
											  `result_id` int(11) NOT NULL AUTO_INCREMENT,
											  `result_stage_id` int(11) NOT NULL,
											  `P1` varchar(4) DEFAULT NULL,
											  `P2` varchar(4) DEFAULT NULL,
											  `P3` varchar(4) DEFAULT NULL,
											  `P4` varchar(4) DEFAULT NULL,
											  `P5` varchar(4) DEFAULT NULL,
											  `P6` varchar(4) DEFAULT NULL,
											  `P7` varchar(4) DEFAULT NULL,
											  `P8` varchar(4) DEFAULT NULL,
											  `P9` varchar(4) DEFAULT NULL,
											  `P10` varchar(4) DEFAULT NULL,
											  `P11` varchar(4) DEFAULT NULL,
											  `P12` varchar(4) DEFAULT NULL,
											  `P13` varchar(4) DEFAULT NULL,
											  `P14` varchar(4) DEFAULT NULL,
											  `P15` varchar(4) DEFAULT NULL,
                        `Y1` varchar(4) DEFAULT NULL,
                        `Y2` varchar(4) DEFAULT NULL,
                        `Y3` varchar(4) DEFAULT NULL,
                        `Y4` varchar(4) DEFAULT NULL,
                        `Y5` varchar(4) DEFAULT NULL,
                        `G1` varchar(4) DEFAULT NULL,
                        `G2` varchar(4) DEFAULT NULL,
                        `G3` varchar(4) DEFAULT NULL,
                        `G4` varchar(4) DEFAULT NULL,
                        `G5` varchar(4) DEFAULT NULL,
                        `M1` varchar(4) DEFAULT NULL,
                        `M2` varchar(4) DEFAULT NULL,
                        `M3` varchar(4) DEFAULT NULL,
                        `M4` varchar(4) DEFAULT NULL,
                        `M5` varchar(4) DEFAULT NULL,
                        PRIMARY KEY (`result_id`),
											  UNIQUE KEY `result_stage_id` (`result_stage_id`)
											) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
			
      $sql_query[] = "DROP TABLE IF EXISTS `yellow_results`";
      $sql_query[] = "DROP TABLE IF EXISTS `green_results`";
      $sql_query[] = "DROP TABLE IF EXISTS `mountain_results`";


        foreach ($sql_query as $query)
        {
            $q = $this->db->query($query);
        }

    }

    function teams_table()
    {

			$sql_query[] = "DROP TABLE IF EXISTS `teams`";
			$sql_query[] = "CREATE TABLE IF NOT EXISTS `teams` (
											  `team_id` int(11) NOT NULL AUTO_INCREMENT,
											  `team_number` int(11) NOT NULL,
											  `team_name` varchar(255) NOT NULL,
                        `team_shortname` varchar(4) NOT NULL,
											  `team_country` varchar(20) NOT NULL,
											  PRIMARY KEY (`team_id`),
											  UNIQUE KEY `team_number` (`team_number`)
											) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1" ;


      $sql_query[] = "REPLACE INTO `teams`
      								(`team_number`,`team_name`,`team_shortname`,`team_country`)
                			VALUES
                			(1,'AG2R La Mondiale','ALM','FRA'),
                			(2,'Astana Pro Team','AST','KAZ'),
                			(3,'BMC Racing Team','BMC','USA'),
                			(4,'Euskaltel – Euskadi','EUS','ESP'),
                			(5,'FDJ – Big Mat','FDJ','FRA'),
                			(6,'Garmin – Barracuda','GRM','USA'),
                			(7,'Greenedge Cycling Team','GRE','AUS'),
                			(8,'Katusha Team','KAT','RUS'),
                			(9,'Lampre – ISD','LAM','ITA'),
                			(10,'Liquigas – Cannondale','LIQ','ITA'),
                			(11,'Lotto Belisol Team','LTB','BEL'),
                			(12,'Movistar Team','MOV','ESP'),
                			(13,'Omega Pharma – Quickstep','OPQ','BEL'),
                			(14,'Rabobank Cycling Team','RAB','NED'),
                			(15,'Radioshack – Nissan','RNT','LUX'),
                			(16,'Sky Procycling','SKY','UK'),
                			(17,'Team Saxo Bank','SAX','DAN'),
                			(18,'Vacansoleil – DCM Pro Cycling Team','VCD','NED'),
                			(19,'Argos – Shimano','ARG','NED'),
                			(20,'Cofidis, le crédit en ligne','COF','FRA'),
                			(21,'Saur – Sojasun','SAU','FRA'),
                			(22,'Team Europcar','EUC','FRA')
                			";

        foreach ($sql_query as $query)
        {
            $q = $this->db->query($query);
        }

    }

    function rider_types_table()
    {
      $sql_query[] = "DROP TABLE IF EXISTS `rider_types`";
      $sql_query[] = "CREATE TABLE IF NOT EXISTS `rider_types` (
                        `type_id` int(11) NOT NULL,
                        `type_description` varchar(255) DEFAULT NULL,
                        PRIMARY KEY (`type_id`)
                      ) ENGINE=InnoDB DEFAULT CHARSET=utf8";

      $sql_query[] = "INSERT INTO `rider_types` (`type_id`, `type_description`)
                      VALUES
                      (1, 'all_rounder'),
                      (2, 'domestique'),
                      (3, 'sprinter'),
                      (4, 'individual_time_trialist'),
                      (5, 'puncheur'),
                      (6, 'climber')";

        foreach ($sql_query as $query)
        {
            $q = $this->db->query($query);
        }

    }

    function selections_table()
    {
      $sql_query[] = "DROP TABLE IF EXISTS `selections`";
      $sql_query[] ="CREATE TABLE IF NOT EXISTS `selections` (
                      `selection_id` int(11) NOT NULL AUTO_INCREMENT,
                      `user_id` int(11) NOT NULL,
                      `S1` int(11) DEFAULT NULL,
                      `S2` int(11) DEFAULT NULL,
                      `S3` int(11) DEFAULT NULL,
                      `S4` int(11) DEFAULT NULL,
                      `S5` int(11) DEFAULT NULL,
                      `S6` int(11) DEFAULT NULL,
                      `S7` int(11) DEFAULT NULL,
                      `S8` int(11) DEFAULT NULL,
                      `S9` int(11) DEFAULT NULL,
                      `S10` int(11) DEFAULT NULL,
                      `S11` int(11) DEFAULT NULL,
                      `S12` int(11) DEFAULT NULL,
                      `S13` int(11) DEFAULT NULL,
                      `S14` int(11) DEFAULT NULL,
                      `S15` int(11) DEFAULT NULL,
                      `R1` int(11) DEFAULT NULL,
                      `R2` int(11) DEFAULT NULL,
                      `R3` int(11) DEFAULT NULL,
                      `R4` int(11) DEFAULT NULL,
                      `R5` int(11) DEFAULT NULL,
                      PRIMARY KEY (`selection_id`),
                      UNIQUE KEY `user_id` (`user_id`)
                    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";

    foreach ($sql_query as $query)
    {
        $q = $this->db->query($query);
    }

  }

    function user_table()
    {
      $sql_query[] = "ALTER TABLE  `users` ADD  `payed` TINYINT( 1 ) NULL DEFAULT NULL AFTER  `active`";
      foreach ($sql_query as $query)
        {
            $q = $this->db->query($query);
        }
    }

    function test()
    {
      $this->load->library('tourpoule');
      $this->tourpoule->calculate_stage(0);
    }

    function test2()
    {
      echo 'Creating 500 accounts';
      for($i=1001;$i<2000;$i++)
      {
      //   $this->ion_auth->register('testuser'.$i, 'passie.01', 'testuser'.$i.'@test.com');
         $this->load->model('selections_model');
         $user_id = $i + 514;
         $selection = '21,22,26,57,62';
         echo $user_id;
         $this->selections_model->update($user_id,array('selection' => $selection, 'replacements' => '55,65'));
      }
    }
   
}